#include "SessionHandler.h"
#include "HelloWorld.h"
#include "GetUserId.h"
#include "IsUserReged.h"
#include "GetSmsPin.h"
#include "AddUserInfo.h"
#include "RequestUnlock.h"
#include "GetUnlockRequest.h"
#include "GetMyDevice.h"
#include "AddDevice.h"
#include "Images.h"
#include "Refresh.h"
#include "UnlockDevice.h"
#include "GetProblems.h"

#include "db/DBManager.h"

#include <QDebug>


QMap<QString, SessionHandler*> SessionHandler::m_mapUrlHandler = QMap<QString, SessionHandler*>();
QThread* SessionHandler::s_mainThread = NULL;
QMap<QString, QString> SessionHandler::s_mapPhoneRandom = QMap<QString, QString>();
QList<QMap<QString,QString>> SessionHandler::s_listRequstUnlock;


void SessionHandler::initDb()
{
    QMap<QString,QString> mapRequest;
    mapRequest.insert("time", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    mapRequest.insert("device_name", "B827EB06C3DB");
    mapRequest.insert("phone", "18915327209");
    s_listRequstUnlock << mapRequest;

    mapRequest.insert("time", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    mapRequest.insert("device_name", "B827EBA7C3AF");
    mapRequest.insert("phone", "18915327209");
    s_listRequstUnlock << mapRequest;

    QVariantMap dbInfo;
    dbInfo.insert("name", "core_config");
    dbInfo.insert("type", "QMYSQL");
    dbInfo.insert("database", "core_config");
    dbInfo.insert("host", "127.0.0.1");
    dbInfo.insert("password", "4rfvBHU*");
    dbInfo.insert("user", "root");
    DBManager::addDatabase(dbInfo);
}

void SessionHandler::initHandler()
{
    m_mapUrlHandler.insert("/helloworld", new HelloWorld);
    m_mapUrlHandler.insert("/isUserReged", new IsUserReged);
    m_mapUrlHandler.insert("/getUserId", new GetUserId);
    m_mapUrlHandler.insert("/users/getSmsPin", new GetSmsPin);
    m_mapUrlHandler.insert("/addUserInfo", new AddUserInfo);

    m_mapUrlHandler.insert("/request_unlock", new RequestUnlock);
    m_mapUrlHandler.insert("/getRequestUnlock", new GetUnlockRequest);
    m_mapUrlHandler.insert("/getMyDevice", new GetMyDevice);
    m_mapUrlHandler.insert("/addDevice", new AddDevice);
    m_mapUrlHandler.insert("/images", new Images);
    m_mapUrlHandler.insert("/refresh", new Refresh);
    m_mapUrlHandler.insert("/unlockDevice", new UnlockDevice);
    m_mapUrlHandler.insert("/getProblems", new GetProblems);
}

void SessionHandler::setMainThread(QThread *thread)
{
    s_mainThread = thread;
}

void SessionHandler::handle(QPointer<JQHttpServer::Session> session)
{
    QString strUrl = session->requestUrl();

    if (strUrl.contains("?"))
    {
        strUrl = strUrl.split("?").first();
    }


    qDebug() << "url: " << strUrl;

    SessionHandler* hand = m_mapUrlHandler.value(strUrl, NULL);

    if (strUrl.startsWith("/images"))
    {
        hand = m_mapUrlHandler.value("/images", NULL);
    }

    if (NULL == hand)
    {
        session->replyText("Invalid handler");
        return;
    }

    hand->setSession(session);
    hand->handleSession();
}

void SessionHandler::initLocalSocket()
{
//    s_localSocket = new QTcpSocket;
//    s_localSocket->moveToThread(s_mainThread);
//    s_localSocket->connectToHost("127.0.0.1", 10001);
}

void SessionHandler::setSession(QPointer<JQHttpServer::Session> &session)
{
    m_session = session;
}
